
//
//  ZYLoginViewController.m
//  ZYOils
//
//  Created by 段新瑞 on 2019/10/4.
//  Copyright © 2019 Mr.Xr. All rights reserved.
//

#import "ZYLoginViewController.h"
#import "AppDelegate.h"
#import "ZYHomeVC.h"
#import "LBTimeButton.h"
#import "ZYRequestUserInfoModel.h"

@interface ZYLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;

@property (weak, nonatomic) IBOutlet UITextField *codeTextField;

@property (weak, nonatomic) IBOutlet LBTimeButton *codeBtn;

@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation ZYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    
    [LBLimitInput limitPhoneInputView:self.phoneNumberTextField];
    
    @weakify(self)
    [[self.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self login];
    }];
    
    [[self.codeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self phoneCode];
    }];
    
    // [self.navigationController pushViewController:[ZYHomeVC new] animated:YES];
    
}

- (void)login {
    [self.view endEditing:YES];
    if ([LBLimitInput isEmpty:self.phoneNumberTextField.text] ) {
        [ZYHud showTipsText:@"请输入手机号"];
        return;
    }
    
    if (![LBLimitInput isValidPhoneNum:self.phoneNumberTextField.text]) {
        [ZYHud showTipsText:@"请输入正确手机号"];
        return;
    }
    
    if ([LBLimitInput isEmpty:self.codeTextField.text] ) {
        [ZYHud showTipsText:@"请输入验证码"];
        return;
    }
    
    [ZYHud showLoadingText:@""];
    NSDictionary *dict = @{@"phoneNO": self.phoneNumberTextField.text,
                           @"phoneCode": self.codeTextField.text};
    [[DataManager manager] postLoginParam:dict block:^(XRCommonModel *model, NSError *error) {
        [ZYHud hideHud];
        if (model.isSuccess) {
            
            ZYRequestUserInfoModel *userModel = [ZYRequestUserInfoModel mj_objectWithKeyValues:model.data];
            ZYUserInfoModel.sharedInfoModel.userModel = userModel;
            [ZYUserInfoModel.sharedInfoModel saveUserModel];
            
            ZYHomeVC *controller = [ZYHomeVC new];
            NSMutableArray *vcLists = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
            [vcLists removeAllObjects];
            [vcLists addObject:controller];
            [self.navigationController setViewControllers:vcLists animated:NO];
            
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}

- (void)phoneCode {
    [self.view endEditing:YES];
    if ([LBLimitInput isEmpty:self.phoneNumberTextField.text] ) {
        [ZYHud showTipsText:@"请输入手机号"];
        return;
    }
    
    if (![LBLimitInput isValidPhoneNum:self.phoneNumberTextField.text]) {
        [ZYHud showTipsText:@"请输入正确手机号"];
        return;
    }
    
    [ZYHud showLoadingText:@""];
    NSDictionary *dict = @{@"phone": self.phoneNumberTextField.text,
                           @"type": @"login"};
    [[DataManager manager] postPhoneCodeParam:dict block:^(XRCommonModel *model, NSError *error) {
        [ZYHud hideHud];
        if (model.isSuccess) {
            [self.codeBtn startTime];
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}

@end
